package com.gzk.hrm.util;

import com.alibaba.fastjson.JSON;
import com.gzk.hrm.exception.GlobalException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.util.Map;

public class HttpClients {

    public static Map<String,Object> doPost(String url){

        PostMethod post = null;
        try {
            HttpClient client = new HttpClient();
            post = new PostMethod(url);
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码

            client.executeMethod(post);
            String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
            System.out.println(result); //打印返回消息状态
            Map<String,Object> token = (Map<String, Object>) JSON.parse(result);
            return token;

        }catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("HTTP请求发送失败！");
        } finally {
            if(post!=null){
                post.releaseConnection();
            }
        }

    }
}
